import os
import dashscope

class Qwen2VL2BInstruct:
    model = "qwen2-vl-2b-instruct"

    messages = []

    def set_messages(self, messages):
        self.messages = messages

    def call_recognition(self, **kwargs):
        if kwargs.get("image_url"):
            image_path = kwargs.get("image_url")

        elif kwargs.get("local_path"):
            image_path = f'file://{kwargs.get("local_path")}'

        else:
            raise ValueError("Either image_url or local_path must be provided.")

        self.messages = [
            {
                "role": "user",
                "content": [
                    {"image": image_path},
                    {"text": "这是什么?"},
                ],
            }
        ]

        return self.call()

    def call(self):
        response = dashscope.MultiModalConversation.call(
            api_key=os.getenv("DASHSCOPE_API_KEY"),
            model=self.model,
            messages=self.messages,
        )
        return response
